package com.ucar.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SpUtil {
	private static final String PREF_NAME = "preferences.ucar";

	private static final String IS_LOGIN = "IS_LOGIN";
	private static final String USER_ID = "USER_ID";

	private static final String TOKEN = "token";
	private static final String NAME = "name";
	private static final String CAR_FLAG = "carflag";
	private static final String REF_CODE = "refcode";
	private static final String MOBILE = "mobile";
	private static final String GENDER = "gender";
	private static final String AD = "ads";

	public static SpUtil instance = null;

	private Context context;

	private SpUtil(Context context) {
		this.context = context;
	}

	public static SpUtil getInstance(Context context) {
		Context applicationContext = context.getApplicationContext();
		if (null == instance || instance.context != applicationContext) {
			instance = new SpUtil(context);
		}
		return instance;
	}

	private SharedPreferences sp;

	public SharedPreferences getSp() {
		if (sp == null)
			sp = context.getSharedPreferences(getSpFileName(),
					Context.MODE_PRIVATE);
		return sp;
	}

	public Editor getEdit() {
		return getSp().edit();
	}

	private String getSpFileName() {
		return PREF_NAME;
	}

	public void logout() {
		// 清空用户信息
		setLogin(false);
		setUserId("");
	}

	public boolean isLogin() {
		return getSp().getBoolean(IS_LOGIN, false);
	}

	public void setLogin(boolean flag) {
		getEdit().putBoolean(IS_LOGIN, flag).commit();
	}

	public void setUserId(String userId) {
		getEdit().putString(USER_ID, userId).commit();
	}

	public String getUserId() {
		return getSp().getString(USER_ID, "");
	}

	public void setName(String name) {
		getEdit().putString(NAME, name).commit();
	}

	public String getName() {
		return getSp().getString(NAME, "");
	}

	public void setCarFlag(String carflag) {
		getEdit().putString(CAR_FLAG, carflag).commit();
	}

	public String getCarFlag() {
		return getSp().getString(CAR_FLAG, "");
	}

	public void setRefCode(String refcode) {
		getEdit().putString(REF_CODE, refcode).commit();
	}

	public String getRefCode() {
		return getSp().getString(REF_CODE, "");
	}

	public void setToken(String token) {
		getEdit().putString(TOKEN, token).commit();
	}

	public String getToken() {
		return getSp().getString(TOKEN, "");
	}

	public void setMobile(String mobile) {
		getEdit().putString(MOBILE, mobile).commit();
	}

	public String getMobile() {
		return getSp().getString(MOBILE, "");
	}
	
	public void setGender(String gender) {
		getEdit().putString(GENDER, gender).commit();
	}

	public String getGender() {
		return getSp().getString(GENDER, "");
	}
	
	public void setAd(String ad) {
		getEdit().putString(AD, ad).commit();
	}

	public String getAd() {
		return getSp().getString(AD, "");
	}
}
